package algorithm;

public class leetcode_0503 {
    public int[] nextGreaterElements(int[] nums) {
        int len=nums.length;
        int[] result=new int[len];
        int[] stack=new int[2*len];
        int current=0;
        for(int i=0;i<len;++i){
            result[i]=-1;
        }
        for(int j=0;j<2;++j){
            for(int i=0;i<len;++i){
                if(current==0||nums[i]<=nums[stack[current-1]]){
                    stack[current]=i;
                    current++;
                }
                else{
                    while(current>=1&&nums[i]>nums[stack[current-1]]){
                        result[stack[current-1]]=nums[i];
                        current--;
                    }
                    stack[current]=i;
                    current++;
                }
            }
        }
        return result;
    }
}
